package com.jackiechancinema.ssm.listener;


import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.jackiechancinema.ssm.dao.WorkerDao;
import com.jackiechancinema.ssm.dto.WorkerDto;
import com.jackiechancinema.ssm.entity.Worker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: Feri(邢朋辉)
 * @time: 2021/10/8 11:04
 */
@Service
public class WorkerReadListener extends AnalysisEventListener<WorkerDto> {
    @Autowired
    private WorkerDao dao;

    private List<Worker> list=new ArrayList<>();
    //获取读取到的每一行对应的对象
    @Override
    public void invoke(WorkerDto dto, AnalysisContext analysisContext) {
//        //实现属性的赋值
        Worker worker = new Worker();
        worker.setWork_name(dto.getWork_name());
        worker.setWork_sex(dto.getWork_sex());
        worker.setWork_birthday(dto.getWork_birthday());
        System.out.println("enter3");
        //添加到集合
//        System.out.println(department);
        list.add(worker);
    }
    //整个读取结束
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
//        System.err.println(list);
        //将读取的数据一次性的存储到数据库
       int r=dao.insertBatch(list);
        list.clear();
    }
}